package com.tepth.busfix.adapter;

import android.content.Context;
import android.support.graphics.drawable.VectorDrawableCompat;
import android.support.v7.widget.AppCompatImageView;
import android.view.View;
import android.widget.CheckBox;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.tepth.busfix.R;
import com.tepth.busfix.bean.reponse.Person;
import com.tepth.busfix.bean.reponse.TroubleContent;

import java.util.List;

/**
 * Created by pc on 2017/3/16.
 */

public class RepairOperatorAdapter extends BaseQuickAdapter<TroubleContent, BaseViewHolder> {

    private int colorRework;
    private int colorNormal;
    private VectorDrawableCompat iconAdd;
    private VectorDrawableCompat iconMinus;

    public RepairOperatorAdapter(Context context, List<TroubleContent> list) {
        super(R.layout.item_repair_operator, list);
        colorRework = context.getResources().getColor(R.color.theme_accent);
        colorNormal = context.getResources().getColor(R.color.white);

        iconAdd = VectorDrawableCompat.create(context.getResources(),
                R.drawable.ic_add_circle_black_24dp, context.getTheme());
        iconMinus = VectorDrawableCompat.create(context.getResources(),
                R.drawable.ic_remove_circle_black_24dp, context.getTheme());
    }

    @Override
    protected void convert(final BaseViewHolder helper, final TroubleContent item) {

        try {
            final CheckBox checkBox = helper.getView(R.id.checkbox);
//            View layoutBg = helper.getView(R.id.layout_bg);
//            layoutBg.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    item.setChecked(!item.isChecked());
//                    checkBox.setChecked(item.isChecked());
//                    OkBus.getInstance().onEvent(RepairActivity.ITEM_SELECT_BUS_TAG, item);
//                }
//            });
            checkBox.setChecked(item.isChecked());
            helper.addOnClickListener(R.id.btn_delete);

            AppCompatImageView ivAdd = helper.getView(R.id.iv_add);
            ivAdd.setImageDrawable(iconAdd);
            helper.addOnClickListener(R.id.iv_add);

            AppCompatImageView ivMinus = helper.getView(R.id.iv_minus);
            ivMinus.setImageDrawable(iconMinus);
            helper.addOnClickListener(R.id.iv_minus);
            helper.setText(R.id.tv_trouble, item.getTroubleTypeName().replaceAll("自定义录入@@", "附加作业项"));
            helper.setText(R.id.tv_operator, item.getOperation().getName());
            TroubleContent.Part part = item.getAccessory() == null ? item.getComponent() : item.getAccessory();
            helper.setText(R.id.tv_part, part.getName());
            helper.setText(R.id.tv_count, item.getUseCount() + "");
            helper.setText(R.id.tv_unit, part.getUnit());
            Person person = item.getWorkPerson();
            View view = helper.getView(R.id.btn_delete);
            View ivComplete = helper.getView(R.id.iv_complete);
            if (person != null) {
                helper.setText(R.id.tv_person, person.getName());
                view.setVisibility(View.GONE);
                ivComplete.setVisibility(View.VISIBLE);
                checkBox.setVisibility(View.INVISIBLE);
                helper.setVisible(R.id.iv_minus, false);
                helper.setVisible(R.id.iv_add, false);

            } else {
                helper.setText(R.id.tv_person, R.string.value_null);
                view.setVisibility(View.VISIBLE);
                ivComplete.setVisibility(View.GONE);
                checkBox.setVisibility(View.VISIBLE);
                helper.setVisible(R.id.iv_minus, true);
                helper.setVisible(R.id.iv_add, true);
            }
            if (item.isRework()) {//被返工的标记
                helper.setBackgroundColor(R.id.layout_bg, colorRework);
            } else {
                helper.setBackgroundColor(R.id.layout_bg, colorNormal);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
